package calorie.com.calorie.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.lxh.baselibray.mvp.MvpActivity;
import com.lxh.baselibray.util.CountDownTimerUtils;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SPUtils;
import com.lxh.baselibray.util.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import calorie.com.calorie.R;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.ModifyDealPwdContact;
import calorie.com.calorie.response.ResultDTO;
import retrofit2.Response;

;

/**
 * 修改交易密码
 */
public class ModifyDealPwdActivity extends MvpActivity<ModifyDealPwdContact.ModifyDealPwdPresent> implements ModifyDealPwdContact.IModifyDealPwd {
    @BindView(R.id.tv_tel)
    TextView tvTel;
    @BindView(R.id.et_security_code)
    EditText etSecurityCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_dealPwd)
    EditText etDealPwd;
    @BindView(R.id.et_confirm_deal_pwd)
    EditText etConfirmDealPwd;

    String phone;
    @Override
    public void initData(Bundle savedInstanceState) {
        phone= SPUtils.getString(Constant.PHONE,null,context);
        tvTel.setText(phone);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_modify_deal_pwd;
    }


    @OnClick({R.id.tv_get_code, R.id.btn_confirm_modify})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.tv_get_code:
                mPresent.sendCode(context,phone);
                break;
            case R.id.btn_confirm_modify:
                String code = etSecurityCode.getText().toString();
                String dealPwd = etDealPwd.getText().toString();
                String confirmDealPwd = etConfirmDealPwd.getText().toString();
                if (ObjectUtils.isEmpty(code)) {
                    ToastUtils.showLongToast(context, "验证码不能为空");
                    return;
                }
                if (ObjectUtils.isEmpty(dealPwd)) {
                    ToastUtils.showLongToast(context, "交易密码不能为空");
                    return;
                }
                if (ObjectUtils.isEmpty(confirmDealPwd)) {
                    ToastUtils.showLongToast(context, "确认交易密码不能为空");
                    return;
                }
                if (!ObjectUtils.equals(dealPwd, confirmDealPwd)) {
                    ToastUtils.showLongToast(context, "两次交易密码不一致");
                    return;
                }
                //showLoadingDialog();
                mPresent.modifyDealPwd(context,phone, confirmDealPwd, code);
                break;
        }
    }

    @Override
    public void getUpdatePassword(Response<ResultDTO> resultDTOResponse) {
        String code = resultDTOResponse.body().getCode();
        String msg = resultDTOResponse.body().getMsg();
        if (code.equals("200")) {
            ToastUtils.showLongToast(context, "修改成功");
            finish();
            return;
        } else if (code.equals("201")) {
            ToastUtils.showLongToast(context, "服务器异常");
            return;
        }
        ToastUtils.showLongToast(context, msg);
    }

    @Override
    public void getSendCodeResult(Response<ResultDTO> resultDTOResponse) {
        String code = resultDTOResponse.body().getCode();
        if (code.equals("200")) {
            CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tvGetCode, 60000, 1000);
            countDownTimerUtils.start();
            ToastUtils.showLongToast(context, "验证码已经发送");
        } else if (code.equals("202")) {
            ToastUtils.showLongToast(context, "账号不存在");
        }
    }

    @Override
    protected ModifyDealPwdContact.ModifyDealPwdPresent createPresent() {
        return new ModifyDealPwdContact.ModifyDealPwdPresent();
    }
}
